<template>
  <div class="container">
    <div class="app-container">
      <!-- 按钮 -->
      <el-button type="primary" size="mini" style="margin:10px" @click="addBtn">添加权限</el-button>
      <!-- 表格 -->
      <el-table default-expand-all row-key="id" :data="list" style="width: 100%">
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column prop="code" label="标识" align="center" />
        <el-table-column prop="description" label="描述" align="center" />
        <el-table-column prop="address" label="操作" align="center">
          <template #default="{ row }">
            <el-button type="text" size="mini" @click="adddialog(row.id)">添加</el-button>
            <el-button type="text" size="mini" @click="editBtn(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="delpower(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增权限点 -->
    <el-dialog
      :title="titleText"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-form ref="formData" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center">
            <el-button size="mini" type="primary" @click="addpower">确 定</el-button>
            <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addPermissionAPI, delPermissionAPI, perDetailAPI, permissionListAPI, putPermissionAPI } from '@/api/permission'
import { tree2List } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      formData: {
        name: '',
        code: '',
        description: '',
        type: 0,
        pid: 0,
        enVisible: '0'
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      titleText: '新增权限点'
    }
  },
  watch: {
    // 监听弹框是否关闭 关闭后清空表单
    dialogVisible(newValue) {
      if (!newValue) {
        this.$refs.formData.resetFields()
        this.formData = {
          name: '',
          code: '',
          description: '',
          type: 0,
          pid: 0,
          enVisible: '0'
        }
      }
    }
  },
  created() {
    this.permissionList()
  },
  methods: {
    async permissionList() {
      // 调用权限列表数据
      const res = await permissionListAPI()

      this.list = tree2List(res.data.data)
    },
    async addpower() {
      this.$refs.formData.validate()

      let res = ''
      if (this.titleText === '新增权限点') {
        // 添加权限点
        res = await addPermissionAPI(this.formData)
      } else {
        res = await putPermissionAPI(this.formData, this.formData.id)
      }

      this.$message.success(res.data.message)
      this.permissionList()
      this.dialogVisible = false
    },
    async delpower(id) {
      // 删除权限功能
      const res = await delPermissionAPI(id)

      this.$message.success(res.data.message)
      this.permissionList()
    },
    addBtn() {
      this.titleText = '新增权限点'
      this.dialogVisible = true
      this.formData.pid = 0
    },
    adddialog(id) {
      this.titleText = '新增权限点'
      // 当点击添加，就获取当前部门 pid，给它添加一个字节点
      this.dialogVisible = true
      this.formData.pid = id
    },
    async editBtn(id) {
      this.titleText = '编辑权限点'

      const res = await perDetailAPI(id)
      this.formData = res.data.data
      // console.log(this.formData)

      this.dialogVisible = true
    }
  }
}

</script>
